﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LiveTiss.Schemas;
using System.Windows.Data;

namespace LiveTiss.Guias.UserControls
{
    public partial class ListaOPMSolicitadasOPMSolicitada : UserControl
    {
        #region Value

        public List<ct_OPMSolicitadasOPMSolicitada> Value
        {
            get { return (List<ct_OPMSolicitadasOPMSolicitada>)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(List<ct_OPMSolicitadasOPMSolicitada>), typeof(ListaOPMSolicitadasOPMSolicitada), new PropertyMetadata(null));

        #endregion

        public ListaOPMSolicitadasOPMSolicitada()
        {
            this.DataContext = this;

            if (this.Value == null)
            {
                this.Value = new List<ct_OPMSolicitadasOPMSolicitada>();
            }

            InitializeComponent();
        }

        protected void btnExcluir_Click(object sender, RoutedEventArgs e)
        {
            var membro = (sender as FrameworkElement).DataContext as ct_OPMSolicitadasOPMSolicitada;

            var list = this.Value.GetRange(0, this.Value.Count);

            list.Remove(membro);

            this.Value = list;
        }

        protected void btnNovo_Click(object sender, RoutedEventArgs e)
        {
            var listaMembros = this.Value.GetRange(0, this.Value.Count);

            listaMembros.Add(new ct_OPMSolicitadasOPMSolicitada()
            {
                fabricante = string.Empty,
                OPM = new ct_tabela()
                {
                    codigo = string.Empty,
                    descricao = string.Empty,
                    tipoTabela = st_tabela.Item00
                },
                quantidadeSolicitada = 0,
                valor = 0,
                valorSpecified = true
            });

            this.Value = listaMembros;
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            var ucOPMSolicitadasOPMSolicitada = (sender as FrameworkElement).FindName("ucOPMSolicitadasOPMSolicitada") as OPMSolicitadasOPMSolicitada;

            var current = (sender as FrameworkElement).DataContext as ct_OPMSolicitadasOPMSolicitada;

            Binding b = new Binding();

            b.Path = new PropertyPath("Current");
            b.Mode = BindingMode.TwoWay;
            b.ValidatesOnDataErrors = true;
            b.NotifyOnValidationError = true;
            b.Source = current;

            ucOPMSolicitadasOPMSolicitada.SetBinding(OPMSolicitadasOPMSolicitada.ValueProperty, b);
        }
    }
}
